/**
 * methods 播放视频控件
 * author :chengguoxu 2021/12/02
 * ersion : 1.0.0
 */
let wsIndexList = [];

/**
 * 
 * @param {*} wsUrl  websocket 连接地址
 * @param {*} containerDom 视频容器
 * @param {*} videoWidth  视频宽度
 * @param {*} videoHeight  视频高度
 */
function playLiveVideo(wsUrl, containerDom) {
    $('#' + containerDom).children('.loading-icon').addClass('show-loading');
    var player = new Player({
        size: {
            width: 610,
            height: 360
        }
    });
    document.getElementById(containerDom).appendChild(player.canvas);
    wsFun(wsUrl, player, containerDom);
}

/**
 * 删除实时视频播放
 */
function closeLiveVideo() {

}

/**
 * 播放视频
 * params {String} wsUrl   websocket 连接地址
 * params {Object} player   播放器容器
 */
function wsFun(wsUrl, player, containerDom) {
    //var strhost = "ws://" + window.location.host + "/wsh264";
    var strhost = "ws://ljfw.ddns.net:7250/";
    // Setup the WebSocket connection and start the player
    var client = new WebSocket(wsUrl);

    client.binaryType = 'arraybuffer';

    client.onopen = function(evt) {
        console.log(evt);
    };
    client.onclose = function(evt) {
        console.log(evt);
    };
    client.onmessage = function(evt) {
        var b = evt.data;
        console.log(b);
        var tag_msg = b.split('$');
        if (tag_msg.length == 4 && tag_msg[1] == "WVideoX264VI") {
            var tag_1 = JSON.parse(tag_msg[3]);
            //var tag_2 = ;				
            let decode = window.atob(tag_1['Msg']['VideoData']);
            var len = decode.length;
            var bytes_1 = new Uint8Array(len);
            for (var i = 0; i < len; i++) {
                bytes_1[i] = decode.charCodeAt(i);
            }
            player.decode(bytes_1);
            if ($('#' + containerDom).children('.loading-icon').hasClass('show-loading')) {
                $('#' + containerDom).children('.loading-icon').removeClass('show-loading');
            }
        } else {
            if (b == "you are connected!") {
                console.log(b);
                var scrible_msg = 'create_subscriber$WVideoX264VI$G2021001${"Msg":{"TopicName":"WVideoX264VI","TopicDomain":"1001","TopicSource":"G2021001"},"TimeStamp":"20210220214742"}';
                client.send(scrible_msg);

            }
        }
    };
    client.onerror = function(evt) {
        onError(evt)
    };
}
export default {
    playLiveVideo,
    closeLiveVideo
}